<?php

namespace app\controllers;

use app\models\Game;
use app\models\Score;
use Yii;
use app\models\People;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\models\SearchPeople;

/**
 * PeopleController implements the CRUD actions for People model.
 */
class PeopleController extends BaseController
{
    public function behaviors()
    {
        $conf = [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
        return array_merge($conf,parent::behaviors());
    }

    /**
     * Lists all People models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new SearchPeople();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single People model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new People model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new People();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing People model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing People model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the People model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return People the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = People::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    public  function actionExport()
    {
        $objectPHPExcel = new \PHPExcel();
        $objectPHPExcel->setActiveSheetIndex(0);

        $n = 0;
        $user = People::findOne(Yii::$app->request->get("uid"));
        if(empty($user)){

        }else{
            $data = $user['games'];
            $total = 0;
            foreach ($data as $product) {

                    //报表头的输出
                    $objectPHPExcel->getActiveSheet()->mergeCells('B1:G1');
                    $objectPHPExcel->getActiveSheet()->setCellValue('B1', '用户对局积分表');

                    $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B2', '用户对局积分表');
                    $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B2', '用户对局积分表');
                    $objectPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getFont()->setSize(24);
                    $objectPHPExcel->setActiveSheetIndex(0)->getStyle('B1')
                        ->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

                    $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B2', '日期：' . date("Y年m月j日"));
//                    $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('G2', '第' . $current_page . '/' . $page_count . '页');
                    $objectPHPExcel->setActiveSheetIndex(0)->getStyle('G2')
                        ->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

                    //表格头的输出
                    $objectPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
                    $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B3', '编号');
                    $objectPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(6.5);
                    $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('C3', '角色');
                    $objectPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(17);
                    $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('D3', '胜负');
                    $objectPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(22);
                    $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('E3', '得分');
                    $objectPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
                    $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('F3', '时间');
                    $objectPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
//                    $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('G3', '在库数');
//                    $objectPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);

                    //设置居中
                    $objectPHPExcel->getActiveSheet()->getStyle('B3:G3')
                        ->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

                    //设置边框
                    $objectPHPExcel->getActiveSheet()->getStyle('B3:G3')
                        ->getBorders()->getTop()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
                    $objectPHPExcel->getActiveSheet()->getStyle('B3:G3')
                        ->getBorders()->getLeft()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
                    $objectPHPExcel->getActiveSheet()->getStyle('B3:G3')
                        ->getBorders()->getRight()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
                    $objectPHPExcel->getActiveSheet()->getStyle('B3:G3')
                        ->getBorders()->getBottom()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
                    $objectPHPExcel->getActiveSheet()->getStyle('B3:G3')
                        ->getBorders()->getVertical()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);

                    //设置颜色
                    $objectPHPExcel->getActiveSheet()->getStyle('B3:G3')->getFill()
                        ->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FF66CCCC');

                //明细的输出
                $objectPHPExcel->getActiveSheet()->setCellValue('B' . ($n + 4), $product['id']);
                $objectPHPExcel->getActiveSheet()->setCellValue('C' . ($n + 4), $product['role']['name']);
                $objectPHPExcel->getActiveSheet()->setCellValue('D' . ($n + 4), $product['wins']['name']);
                $obj_score = Score::findOne(['role_id'=>$product['role_id'],'win'=>$product['win']]);
                if(empty($obj_score)){
                    $score = 0;
                }else{
                    $score = $obj_score['score'];
                }
                $total += $score;
                $objectPHPExcel->getActiveSheet()->setCellValue('E' . ($n + 4), $score);
                $objectPHPExcel->getActiveSheet()->setCellValue('F' . ($n + 4), $product['create_at']);
//                $objectPHPExcel->getActiveSheet()->setCellValue('G' . ($n + 4), 6);
                //设置边框
                $currentRowNum = $n + 4;
                $objectPHPExcel->getActiveSheet()->getStyle('B' . ($n + 4) . ':G' . $currentRowNum)
                    ->getBorders()->getTop()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
                $objectPHPExcel->getActiveSheet()->getStyle('B' . ($n + 4) . ':G' . $currentRowNum)
                    ->getBorders()->getLeft()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
                $objectPHPExcel->getActiveSheet()->getStyle('B' . ($n + 4) . ':G' . $currentRowNum)
                    ->getBorders()->getRight()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
                $objectPHPExcel->getActiveSheet()->getStyle('B' . ($n + 4) . ':G' . $currentRowNum)
                    ->getBorders()->getBottom()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
//                $objectPHPExcel->getActiveSheet()->getStyle('B' . ($n + 4) . ':G' . $currentRowNum)
//                    ->getBorders()->getVertical()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
                $n = $n + 1;
            }
            $objectPHPExcel->getActiveSheet()->setCellValue('D' . ($n + 5), "总分：");
            $objectPHPExcel->getActiveSheet()->setCellValue('E' . ($n + 5), $total);

            //设置分页显示
            //$objectPHPExcel->getActiveSheet()->setBreak( 'I55' , PHPExcel_Worksheet::BREAK_ROW );
            //$objectPHPExcel->getActiveSheet()->setBreak( 'I10' , PHPExcel_Worksheet::BREAK_COLUMN );
            $objectPHPExcel->getActiveSheet()->getPageSetup()->setHorizontalCentered(true);
            $objectPHPExcel->getActiveSheet()->getPageSetup()->setVerticalCentered(false);
        }



        ob_end_clean();
        ob_start();

        header('Content-Type : application/vnd.ms-excel');
        header('Content-Disposition:attachment;filename="' . '用户对局积分表-' . date("Y年m月j日") . '.xls"');
        $objWriter = \PHPExcel_IOFactory::createWriter($objectPHPExcel, 'Excel5');
        $objWriter->save('php://output');
    }
}
